package gui.panels;

import domain.entities.ChatMessage;
import domain.services.ChatMessageDomainService;
import gui.SubPanel;
import util.DialogHelper;
import util.StringValidator;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.Date;
import java.util.Vector;

public class ChatMessageSubPanel extends SubPanel {
    JTable table = new JTable();
    JScrollPane scrollPane = new JScrollPane(table);
    DefaultTableModel model = new DefaultTableModel();

    JLabel startDateLabel = new JLabel("开始日期:");
    JTextField startDateInput = new JTextField();
    JLabel endDateLabel = new JLabel("结束日期:");
    JTextField endDateInput = new JTextField();

    JButton queryButton = new JButton("查询全部");
    JButton rangeQueryButton = new JButton("范围查找");

    {
        Vector<String> vName = new Vector<>();
        vName.add("id");
        vName.add("用户id");
        vName.add("消息时间");
        vName.add("消息内容");
        model.setColumnIdentifiers(vName);
    }


    public ChatMessageSubPanel() {
        String nowDateStr = new Date(System.currentTimeMillis()).toString();
        queryButton.setBounds(100, 60, 100, 30);
        scrollPane.setBounds(100, 120, 800, 300);
        startDateLabel.setBounds(250, 60, 60, 30);
        startDateInput.setBounds(320, 60, 150, 30);
        startDateInput.setText(nowDateStr);
        endDateLabel.setBounds(490, 60, 60, 30);
        endDateInput.setBounds(560, 60, 150, 30);
        endDateInput.setText(nowDateStr);
        rangeQueryButton.setBounds(730, 60, 100, 30);
        this.add(queryButton);
        this.add(scrollPane);
        this.add(startDateLabel);
        this.add(startDateInput);
        this.add(endDateLabel);
        this.add(endDateInput);
        this.add(rangeQueryButton);
        loadMessage();
        queryButton.addActionListener(e -> {
            loadMessage();
        });
        rangeQueryButton.addActionListener(e -> {
            String startDateStr = startDateInput.getText();
            String endDateStr = endDateInput.getText();
            if (StringValidator.isBlank(startDateStr)) {
                DialogHelper.showWaringDialog("请输入开始时间");
                return;
            }
            if (StringValidator.isBlank(endDateStr)) {
                DialogHelper.showWaringDialog("请输入结束时间");
                return;
            }
            try {
                long startDate = Date.valueOf(startDateStr).getTime();
                long endDate = Date.valueOf(endDateStr).getTime();
                if (endDate - startDate < 0) {
                    DialogHelper.showWaringDialog("结束时间不能比开始时间早");
                    return;
                }
                clearTable();
                long oneDayTime = 60 * 60 * 24 * 1000;
                ChatMessageDomainService.Instance.getAllMessage().stream()
                        .filter(m -> m.getMessageDate().getTime() >= startDate &&
                                m.getMessageDate().getTime() <= endDate + oneDayTime)
                        .forEach(this::addMessageToTable);

            } catch (Exception exception) {
                DialogHelper.showWaringDialog("输入日期格式有误\n日期格式为yyyy-mm-dd(年-月-日)");
            }
        });
    }

    void loadMessage() {
        clearTable();
        ChatMessageDomainService.Instance.getAllMessage().forEach(this::addMessageToTable);
    }

    void clearTable() {
        while (model.getRowCount() > 0) {
            model.removeRow(model.getRowCount() - 1);
        }
    }


    void addMessageToTable(ChatMessage m) {
        Object[] obj = new Object[]{m.getId(), m.getUserId(), m.getMessageDate(), m.getContent()};
        model.addRow(obj);
        table.setModel(model);
    }
}
